import multiprocessing as mp
from tools.log import log
from tools.config import Config as cfg
from car_base import clientMode
from car_base.car_drive import carDataThreadRun, CarDrive
from tools.databus.bus import workFlow
import threading
import time

class CustomizeTask():
    """智慧交通自动行驶控制插件"""
    def __init__(self, cline):
        pass


    def worker(self, state_dict, stopFlag, q_send):
        """
        智慧交通自动行驶任务执行进程
        :param state_dict: 全局状态dict（可获取无人车自动行驶任务启动标志）
        :param stopFlag: 无人车反馈的状态位
        :param q_send: 无人车控制指令消息队列
        """
        cline = clientMode()  # 获取连接模式
        carDataThreadRun(cline, stopFlag, q_send)  # 启动数据接收和发送线程
        car = CarDrive(stopFlag, q_send)
        while True:
            if state_dict[cfg.CAR_TASK] == "TASK1":
                log.info("智慧交通自动行驶任务一执行中！！")
                # 无人车自动行驶任务
                car.go()
                car.back()
                car.left()
                car.right()
                car.track()
                state_dict[cfg.CAR_TASK] = ""
            elif state_dict[cfg.CAR_TASK] == "TASK2":
                time.sleep(0.5)
                car.buzzer(0x01)
                time.sleep(0.5)
                car.buzzer(0x00)

    def onExit(self):
        pass


def carTaskPluginRegist(state_dict, stopFlag, q_send):
    cline = clientMode()  # 获取连接模式
    car_task_plugin = CustomizeTask(cline)

    workFlow.registBus(car_task_plugin.__class__.__name__,
                       car_task_plugin.worker,
                       (state_dict, stopFlag, q_send),
                       car_task_plugin.onExit)

